package com.ruoyi.rms.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.rms.domain.DishesMessage;
import com.ruoyi.rms.domain.OrderMessage;
import com.ruoyi.rms.enums.DishesStatus;
import com.ruoyi.rms.enums.OrderStatus;
import com.ruoyi.rms.service.IDishesMessageService;
import com.ruoyi.rms.service.IOrderMessageService;
import com.ruoyi.rms.utils.RetUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rms.mapper.DishesOrderMapper;
import com.ruoyi.rms.domain.DishesOrder;
import com.ruoyi.rms.service.IDishesOrderService;

/**
 * 点餐信息Service业务层处理
 * 
 * @author wyl
 * @date 2024-03-11
 */
@Service
@Slf4j
public class DishesOrderServiceImpl implements IDishesOrderService 
{
    @Autowired
    private DishesOrderMapper dishesOrderMapper;

    @Autowired
    private IOrderMessageService orderMessageService;

    @Autowired
    private IDishesMessageService dishesMessageService;

    /**
     * 查询点餐信息
     * 
     * @param orderId 点餐信息主键
     * @return 点餐信息
     */
    @Override
    public List<DishesOrder> selectDishesOrderByOrderId(Long orderId)
    {
        return dishesOrderMapper.selectDishesOrderByOrderId(orderId);
    }

    /**
     * 查询点餐信息列表
     * 
     * @param dishesOrder 点餐信息
     * @return 点餐信息
     */
    @Override
    public List<DishesOrder> selectDishesOrderList(DishesOrder dishesOrder)
    {
        return dishesOrderMapper.selectDishesOrderList(dishesOrder);
    }

    /**
     * 根据订单信息查询点餐情况
     * @param orderId
     * @return
     */
    @Override
    public List<DishesMessage> selectDishesByOrderId(Long orderId) {
        List<DishesOrder> dishesOrderList = dishesOrderMapper.selectDishesOrderByOrderId(orderId);
        if (dishesOrderList.isEmpty()) return null;

        List<DishesMessage> list = new ArrayList<>();
        for (DishesOrder dishesOrder : dishesOrderList) {
            Long dishesId = dishesOrder.getDishesId();
            Integer dishesAccount = dishesOrder.getDishesAccount();
            DishesMessage dishesMessage = dishesMessageService.selectDishesMessageByDishesId(dishesId);
            dishesMessage.setOrderNum(dishesAccount);
            list.add(dishesMessage);
        }

        return list;
    }

    /**
     * 新增点餐信息
     * @param orderId 订餐主键
     * @param dishesOrderMap 菜品及其数量
     * @return
     */
    @Override
    public int insertDishesOrder(Long orderId, Map<Long, Integer> dishesOrderMap)
    {
        OrderMessage orderMessage = orderMessageService.selectOrderMessageByOrderId(orderId);
        String status = orderMessage.getStatus();
        if (OrderStatus.PAYED.getStatus().equals(status) || OrderStatus.TERMINATED.getStatus().equals(status)) {
            log.error("此订单已付款或已被终止，无法点餐");
            return RetUtils.ERROR;
        }

        for (Map.Entry<Long, Integer> entry : dishesOrderMap.entrySet()) {
            Long dishesId = entry.getKey();
            int dishesAccount = entry.getValue();

            DishesMessage dishesMessage = dishesMessageService.selectDishesMessageByDishesId(dishesId);
            int account = dishesMessage.getAccount();
            if (dishesAccount > account) {
                log.error("菜品不足，下单失败");
                return RetUtils.ERROR;
            }

            // 同步更新菜品数量
            dishesMessage.setAccount(account - dishesAccount);
            if (account-dishesAccount == 0) {
                // 状态设置为售空
                dishesMessage.setStatus(DishesStatus.SELL_OUT.getStatus());
            }
            dishesMessageService.updateDishesMessage(dishesMessage);

            DishesOrder dishesOrder = dishesOrderMapper.selectDishesOrderByOrderIdAndDishesId(orderId, dishesId);
            if (dishesOrder == null) {
                // 如果还未产生下单记录，则直接下单
                dishesOrder = new DishesOrder();
                dishesOrder.setOrderId(orderId);
                dishesOrder.setDishesId(dishesId);
                dishesOrder.setDishesAccount(dishesAccount);
                dishesOrderMapper.insertDishesOrder(dishesOrder);
            }
            else {
                // 如果已经对该菜品下过单，则只需追加数量
                dishesOrder.setDishesAccount(dishesOrder.getDishesAccount() + dishesAccount);
                dishesOrderMapper.updateDishesOrder(dishesOrder);
            }
        }

        // 更新订单状态为已下单状态
        orderMessage.setStatus(OrderStatus.ORDERED.getStatus());
        orderMessageService.updateOrderMessageByWxId(orderMessage);
        return RetUtils.OK;
    }

    /**
     * 修改点餐信息
     * 
     * @param dishesOrder 点餐信息
     * @return 结果
     */
    @Override
    public int updateDishesOrder(DishesOrder dishesOrder)
    {
        return dishesOrderMapper.updateDishesOrder(dishesOrder);
    }

    /**
     * 批量删除点餐信息
     * 
     * @param orderIds 需要删除的点餐信息主键
     * @return 结果
     */
    @Override
    public int deleteDishesOrderByOrderIds(Long[] orderIds)
    {
        return dishesOrderMapper.deleteDishesOrderByOrderIds(orderIds);
    }

    /**
     * 删除点餐信息信息
     * 
     * @param orderId 点餐信息主键
     * @return 结果
     */
    @Override
    public int deleteDishesOrderByOrderId(Long orderId)
    {
        return dishesOrderMapper.deleteDishesOrderByOrderId(orderId);
    }
}
